package com.ruoyi.report.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.report.domain.ReportWorkbookConfig;
import com.ruoyi.report.domain.ReportWorkbookData;
import com.ruoyi.report.mapper.ReportDataMapper;
import com.ruoyi.report.mapper.ReportWorkbookDataMapper;
import com.ruoyi.report.service.IReportDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: report
 * @ClassName: ReportDataServiceImpl
 * @Description: 报表数据service层实现
 * @Author: sunyan
 * @Date: 2022/3/2 16:02
 */
@Service
public class ReportDataServiceImpl implements IReportDataService {

    @Autowired
    ReportDataMapper reportDataMapper;

    @Autowired
    ReportWorkbookDataMapper reportWorkbookDataMapper;

    @Override
    public List<ReportWorkbookConfig> selectReportDataList(ReportWorkbookConfig reportWorkbookConfig) {
        return reportDataMapper.selectReportDataList(reportWorkbookConfig);
    }

    @Override
    public ReportWorkbookConfig selectReportById(Long id) {
        ReportWorkbookConfig reportWorkbookConfig = reportDataMapper.selectReportById(id);
        List<String> list = reportWorkbookDataMapper.selectReportWorkbookDataByReportId(id);
        JSONObject js = JSON.parseObject(reportWorkbookConfig.getReportData());
        js.put("data", list);
        String s = JSON.toJSONString(js);
        reportWorkbookConfig.setReportData(s);
//        System.out.println(reportWorkbookConfig);
        return reportWorkbookConfig;
    }

    @Override
    public Long insertReportData(ReportWorkbookConfig reportWorkbookConfig) {
        reportWorkbookConfig.setCreateBy(SecurityUtils.getUsername());
        reportDataMapper.insertReportData(reportWorkbookConfig);
        ReportWorkbookData reportWorkbookData = new ReportWorkbookData();
        JSONArray l = JSON.parseArray(reportWorkbookConfig.getSheetData());
        //循环数组初始化reportWorkbookData对象
        for(int i = 0; i < l.size(); i++){
            JSONObject object = l.getJSONObject(i);
            reportWorkbookData.setSheetIndex((String)object.get("index"));
            reportWorkbookData.setSheetName((String) object.get("name"));
            reportWorkbookData.setSheetData(object.toJSONString());
            reportWorkbookData.setReportId(reportWorkbookConfig.getId());
            reportWorkbookData.setUpdateBy(SecurityUtils.getUsername());
            reportWorkbookDataMapper.insertReportWorkbookData(reportWorkbookData);
        }

        return reportWorkbookConfig.getId() ;
    }

    @Override
    public Long copyInsert(ReportWorkbookConfig reportWorkbookConfig) {
        reportWorkbookConfig.setCreateBy(SecurityUtils.getUsername());
        reportDataMapper.insertReportData(reportWorkbookConfig);
        ReportWorkbookData reportWorkbookData = new ReportWorkbookData();
        String s = reportWorkbookConfig.getSheetData();
        JSONArray l = JSON.parseArray(reportWorkbookConfig.getSheetData());
        System.out.println(l);
        //循环数组初始化reportWorkbookData对象
        for(int i = 0; i < l.size(); i++){
            String str = l.getString(i);
            JSONObject object = JSON.parseObject(str);
            reportWorkbookData.setSheetIndex((String)object.get("index"));
            reportWorkbookData.setSheetName((String) object.get("name"));
            reportWorkbookData.setSheetData(object.toJSONString());
            reportWorkbookData.setReportId(reportWorkbookConfig.getId());
            reportWorkbookData.setUpdateBy(SecurityUtils.getUsername());
            reportWorkbookDataMapper.insertReportWorkbookData(reportWorkbookData);
        }
        return reportWorkbookConfig.getId();
    }

    @Override
    @Transactional
    public int updateReportData(ReportWorkbookConfig reportWorkbookConfig) {
        ReportWorkbookData reportWorkbookData = new ReportWorkbookData();
        JSONArray l = JSON.parseArray(reportWorkbookConfig.getSheetData());
        reportWorkbookDataMapper.deleteReportWorkbookDataByReportId(reportWorkbookConfig.getId());
        //循环数组初始化reportWorkbookData对象
        for(int i = 0; i < l.size(); i++){
            JSONObject object = l.getJSONObject(i);
            reportWorkbookData.setSheetIndex((String) object.get("index"));
            reportWorkbookData.setSheetName((String) object.get("name"));
            reportWorkbookData.setSheetData(object.toJSONString());
            reportWorkbookData.setReportId(reportWorkbookConfig.getId());
            reportWorkbookData.setUpdateBy(SecurityUtils.getUsername());
            reportWorkbookDataMapper.insertReportWorkbookData(reportWorkbookData);
        }
        reportWorkbookConfig.setUpdateBy(SecurityUtils.getUsername());
        return reportDataMapper.updateReportData(reportWorkbookConfig);
    }

    @Override
    public int deleteReportById(Long id) {
        return reportDataMapper.deleteReportById(id);
    }

    @Override
    public int setReportTemplate(ReportWorkbookConfig reportWorkbookConfig){
        return reportDataMapper.setReportTemplate(reportWorkbookConfig);
    }
}
